<?php
class ml_tool_tag
{
    static function tgidDecode($tgid)
    {
        $sTgid = (string)$tgid;
        return array(
            'tag_group_id' => substr($sTgid , 0 , 4),
            'tag_id' => substr($sTgid , 4)
        );
    }
    static function tgidEncode($group_id , $tag_id)
    {
        $group_id = (int)$group_id;
        if($group_id<=9999 && $group_id>1000)
        {
            return (string)$group_id.(string)$tag_id;
        }
        return false;
    }
    
    static function tagidEncode($tagid){
        if(empty($tagid))
            return false;
        $tgid = base_convert($tagid, 10, 36);
        $tgid = str_pad($tgid, 3, '0', STR_PAD_LEFT);
        return $tgid;
    }
    static function tagidDecode($tgid){
        $len = strlen($tgid);
        $size = $len/3;
        $tag_array = array();
        for($i=0;$i<$size;$i=$i+3){
            $tag_array = base_convert(sub_str($tgid, $i, 3),36,10);
        }
        return $tag_array;
    }

    static function comb_key($option){
        $where = array();
        if(!empty($option['ctg'])){
            $type = self::type_prefix('ctg').$option['ctg'];
            $where[] = array('name'=>'ctg', 'value'=>$option['ctg']);
            unset($option['ctg']);
        }else if(!empty($option['tag'])){
            $type .= trim(self::type_prefix('tag'),'_');
        }else{
            $type .= trim(self::type_prefix('guang'),'_');
        }
        $key_array[0] = $type;
            
        if(is_array($option['tag'])){
            foreach($option['tag'] as $k=>$tag){
                $tag_array[self::all_sort($k)] = $tag;
                $where[] = array('name'=>$k, 'value'=>$tag);
            }
            ksort($tag_array);
            $tag_comb = implode('', $tag_array);
            $key_array[self::all_sort('tag')] = $tag_comb;
            unset($option['tag']);
        }
        if(is_array($option)){
            foreach($option as $key=>$v){
                $key_array[self::all_sort($key)] = self::type_prefix($key).$v;
                $where[] = array('name'=>$key, 'value'=>$v);
            }
        }
        ksort($key_array);
        $comb_key = implode('_', $key_array);
        $count = count($where);
        if( $count == 0){
            $name = array('name'=>'guang', 'value'=>'');
        }else if($count == 1){
            $name = $where[0];
        }else{
            $name = false;
        }
        return array('comb_key'=>$comb_key, 'name'=>$name);
    }
    static function single_key($name, $value){
        $type = self::type_prefix($name);
        $single_key = $type.$value;
        return $single_key;
    }
    static function type_prefix($name){
        $array = array('ctg'=>'c_', 'color'=>'co','sex'=>'sex', 'price'=>'pr', 'guang'=>'guang', 'tag'=>'t_');
        $type = $array[$name];
        if(empty($type))
            $type = 't_';
        return $type;
    }
    static function all_sort($name){
        $array = array(
            'ctg'=>1,
            'tag'=>2,
            'guang_ctg_word'=>3,
            'guang_style_word'=>4,
            'guang_element_word'=>5,
            'guang_material'=>6,
            'color'=>7,
            'price'=>8,
            'sex'=>9,
        );
        return $array[$name];
    }

}